{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "def luhn_algorithm(purportedCC=''):\n",
    "    sum = 0\n",
    "    for i, digit in enumerate([int(x) for x in purportedCC[::-1]]):\n",
    "        if i % 2 == 0:\n",
    "            digit *= 2\n",
    "            if digit > 9:\n",
    "                digit -= 9\n",
    "        sum += digit\n",
    "    return str(sum * 9)[-1]\n",
    "\n",
    "%time print(luhn_algorithm('78639329'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "78639329\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "re = ''\n",
    "for i in range(8):\n",
    "    num = random.randint(0,9)\n",
    "    re = re + str(num)\n",
    "print(re)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "786393298\n"
     ]
    }
   ],
   "source": [
    "num = '78639329'\n",
    "sum = 0\n",
    "for i in range(len(num)):\n",
    "    if i % 2 == 0:\n",
    "        d = int(num[::-1][i]) * 2\n",
    "        d = int(d) // 10 + int(d) % 10\n",
    "    else:\n",
    "        d = int(num[::-1][i])\n",
    "    sum += d\n",
    "print(num + str(sum * 9)[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = d // 10 + d % 10\n",
    "\n",
    "if d >= 10:\n",
    "    d = d -9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "786393298\n",
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%timeit\n",
    "num = '78639329'\n",
    "sum = 0\n",
    "for i,d in enumerate([int(d) for d in num[::-1]]):\n",
    "    if i % 2 == 0:\n",
    "        d = d * 2\n",
    "        d = d // 10 + d % 10\n",
    "    sum += int(d)\n",
    "%time print(num + str(sum * 9)[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 8, 6, 3, 9, 3, 2, 9]"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num = '78639329'\n",
    "[int(d) for d in num]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#假设存在m\n",
    "m = 10 * a + b (10 > a >= 0,10 > b >= 0)\n",
    "\n",
    "#m取10的模，结果自然为b\n",
    "m % 10 = b\n",
    "#m乘以9，m的个位数自然就是9b的个位数\n",
    "9 * m = 90 * a + 9 * b\n",
    "\n",
    "#如果b == 0，那么自然就是取 0\n",
    "#如果b > 0\n",
    "#第一种方法结果取(10 - b)，b = 1,10 - b == 9 * b\n",
    "#9b大于10的情况\n",
    "9 * b = 10 * b - b\n",
    "      = 10 * (b - 1) + 10 - b\n",
    "      = 10 * (b - 1) + (10 - b)\n",
    "#去个位数，就是 10 - b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
